﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPSTrackingWebsiteManager.ClassManager;
using Telerik.Web.UI;


namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class UsersByCompanyIDSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent(List<GridDataItem> rows);
        public event AcceptEvent OnAcceptEvent;

        public delegate void CancelEvent();
        public event CancelEvent OnCancelEvent;

        //Propeties
        [Browsable(true), Category("Custom")]
        public bool IsMultipleSelects { get; set; }

        [Browsable(true), Category("Custom")]
        public string CalledBy { get; set; }

        [Browsable(true), Category("Custom")]
        public bool IsClearGridAfterClose { get; set; }

        //Events
        protected void Page_Load(object sender, EventArgs e)
        {
            UsersGrid.AllowMultiRowSelection = IsMultipleSelects;

            if (!IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = "Name";
                expression.SetSortOrder("Ascending");
                this.UsersGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                if (IsClearGridAfterClose)
                    clearUsersGrid();
            }
        }

        protected void btnUsersSearch_Click(object sender, EventArgs e)
        {
            getInfoGrid();
            UsersGrid.DataBind();
        }

        protected void GetUsersGridToAssociate()
        {
            UsersGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.UsersProvider.GetByCompanyIDToAssociateGPSGroupsFind(int.Parse(HttpContext.Current.Session["CompanyID"].ToString()), txtSearchUsername.Text, txtSearchName.Text, txtSearchFirstName.Text, txtSearchLastName.Text);
        }

        protected void btnUsersOK_Click(object sender, EventArgs e)
        {
            List<GridDataItem> rows = new List<GridDataItem>();
            foreach (Telerik.Web.UI.GridDataItem selectedItem in UsersGrid.SelectedItems)
            {
                rows.Add(selectedItem);
            }

            if (IsClearGridAfterClose)
                clearUsersGrid();

            OnAcceptEvent(rows);
        }

        protected void btnUsersCancel_Click(object sender, EventArgs e)
        {
            if (IsClearGridAfterClose)
                clearUsersGrid();
            OnCancelEvent();
        }

        //Methods

        public void clearUsersGrid()
        {
            UsersGrid.DataSource = new string[] { };
            UsersGrid.DataBind();
        }

        protected void getInfoGrid()
        {
            if (CalledBy == "UsersByGPSGroups")
            {
                GetUsersGridToAssociate();
            }
        }

        /*protected void btnUsersOK_Click(object sender, EventArgs e)
        {
            GPSTrackingDBV2.Entities.UsersByGpsGroups UsersByGpsGroupsEntity = new GPSTrackingDBV2.Entities.UsersByGpsGroups();
            
            int gpsGroupID;
            int companyID;

            try
            {
                gpsGroupID = int.Parse(HttpContext.Current.Session["GPSGroupID"].ToString());
                companyID = int.Parse(HttpContext.Current.Session["CompanyID"].ToString());
            }
            catch (Exception ex)
            {
                showMessage("No se ha seleccionado un grupo de GPS, debe seleccionar uno antes de asociar los usuarios.");
                return;
            }
 
            foreach (Telerik.Web.UI.GridDataItem selectedItem in UsersGrid.SelectedItems)
            {
                try
                {
                    SessionManager objSessionManager = new SessionManager();
                    objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

                    //tm.BeginTransaction();
                    UsersByGpsGroupsEntity.Dispose();
                    UsersByGpsGroupsEntity.GpsGroupId = gpsGroupID;
                    UsersByGpsGroupsEntity.Username = selectedItem["Username"].Text;
                    UsersByGpsGroupsEntity.CompanyId = objSessionManager.CompanyIDSelectedComboBox;

                    GPSTrackingDBV2.Data.DataRepository.UsersByGpsGroupsProvider.Insert(UsersByGpsGroupsEntity);
                    //tm.Commit();
                }
                catch (Exception ex)
                {
                    //tm.Rollback();
                    //ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref GPSGrid);
                }
            }
            GetUsersGridToAssociate();
            OnAcceptEvent();
        }*/

        protected void UsersGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            getInfoGrid();
        }
    }
}